#include "constants.h";

XfMissionChapterExecute={
	private ["_chapter_succeeded"];
	diag_log "";
	diag_log "=========================================================================";
	diag_log "                             CHAPTER 2                                   ";
	diag_log "=========================================================================";
	diag_log "";
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter2.sqf","XfMissionChapterExecute","Starting Chapter","_last_random_reinforcement","_random_reinf_diff","_reinforcement_power"];
	DM_Mission_Chapter_CanRun = True;
	DM_Mission_Chapter_Runs = True;
	DM_Mission_Chapter_Success = 0;
	call XfMissionChapterSetup;
	_chapter_succeeded = false;
	if (!DM_Mission_Mission_CanRun) exitWith {diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter2.sqf","XfMissionChapterSetup","Mission canceled!"];};

	_random_town = DM_Chapter_Location;
	_random_town_pos = _random_town select XF_MAP_LOCATION_CENTER;
	_random_town_pos_size = _random_town select XF_MAP_LOCATION_SIZE;
	
	_enemy_groups = DM_Target;
	_convoy_pos = DM_Chapter_Position;
	// Helico landing position
	_mike_helico = [[17478.6,6090.94,0.1],184.626];
	_ally_convoy = [];
	_allied_groups = [];
	_ally_damage = false;
	_reinforcement = false;
	_time_reinf = time + 100000;
	
	_players = call XPlayersNumber;
	_enemy_troops = _players + DM_Mission_EnemyPower + DM_Mission_EnemyCoeff;

	while { (count _ally_convoy) < 20 } do {
		_grpSoldiers = [d_own_side,[_convoy_pos,30],["chapter2_convoy","combat","buildings","fortify","noveh","nofollow"],0] call XfMissionCreateFootmenGroup;
		{
			_ally_convoy set [ count _ally_convoy, _x ];
			_x allowDamage _ally_damage;
		} foreach units _grpSoldiers;
		_allied_groups set [ count _allied_groups, _grpSoldiers ];
	};
	diag_log Format["(%1)[%2]: %3; %4: %5",diag_tickTime,"chapter2.sqf","XfMissionChapterSetup","Ally Convoy",_ally_convoy];
	
	{
		_x setBehaviour "STEALTH";
		(leader _x) doMove _convoy_pos;
	} foreach _enemy_groups;
	
	_path = DM_Chapter_Data;
	_idx = 1;
	_dist = 0;
	_current = _path select 0;
	while { _dist < 300 } do {
		_dist = _dist + (_current distance (_path select _idx));
		_idx = _idx + 1;
	};
	_vehicles_start = _idx;
	_vehicles_amount = ceil(_players / 3);
	
	diag_log Format["(%1)[%2]: %3: %4: %5, %6",diag_tickTime,"chapter2.sqf","XfMissionChapterExecute","Entering the loop",[_random_town, count _ally_convoy, count _enemy_groups], Time];
	while {DM_Mission_Chapter_CanRun AND DM_Mission_Mission_CanRun} do {
		sleep 5;
		_enemy_counts = ([_random_town_pos,_random_town_pos_size * 2,d_enemy_side] call XfMissionGetZoneSideCount) select 0;
		_active_enemy = 0;
		
		_ally_alive = 0;
		{
			if (!isnull _x) then {
				if (alive _x) then { _ally_alive = _ally_alive + 1; };
			};
		} foreach _ally_convoy;
		
		_players_found = true;
		if (X_MP) then {
			_players_found = false;
			{
				if (([(getPos (vehicle _x)), _random_town_pos, _random_town_pos_size * 4] call XfPointInCircle) && isplayer _x && alive _x) exitWith { _players_found = true; };
			}foreach playableUnits;
		} else {
			if (alive player) then { _players_found = ([(getPos (vehicle player)), _random_town_pos, _random_town_pos_size * 4] call XfPointInCircle); };
		};
		
		if (!_reinforcement && time > _time_reinf) then {
			_reinforcement = true;
			"STR_MF_M_HAMMER1_2_REINFORCEMENT" call XfMissionMessageToAll;
			// [d_own_side,[_convoy_pos,80],"PARA",3] call XfMissionCallReinforcement;
			["reinforcement_canceled",False] call XNetSetJIP;
			[d_own_side,_convoy_pos,80,3,0,[]] spawn XfMissionReinforcementPara;
		};
		
		if (_players_found && !_ally_damage) then {
			_ally_damage = true;
			"STR_MF_M_HAMMER1_2_SMOKESHELL" call XfMissionMessageToAll;
			{
				if (!isnull _x) then {
					_x allowDamage _ally_damage;
				};
			} foreach _ally_convoy;
			{
				if (!isnull _x) then {
					_pos = getPos (leader _x);
					if (alive (leader _x) && (_pos distance _convoy_pos) < 200) then {
						("SmokeShellBlue" createVehicle _pos) setDamage 1;
					};
				};
			} foreach _allied_groups;
			{
				if (!isnull _x) then {
					_pos = getPos (leader _x);
					if (alive (leader _x) && (_pos distance _convoy_pos) < 200) then {
						("SmokeShellRed" createVehicle _pos) setDamage 1;
					};
				};
			} foreach _enemy_groups;
			_time_reinf = time + 90;
		};
	
		{
			if (!isnull _x) then {
				_grp = _x;
				{ if (alive _x) then { _active_enemy = _active_enemy + 1; };
				} foreach units _grp;
			};
		} foreach _enemy_groups;
		
		if (_ally_alive <= 0) then {
			_chapter_succeeded = false;
			DM_Mission_Chapter_CanRun = false;
		} else {
			if (_enemy_counts <= 4 && _active_enemy <= 4 && _enemy_troops <= 0) then {
				_chapter_succeeded = true;
				DM_Mission_Chapter_CanRun = false;
			};
		};
		
		if (DM_Mission_Chapter_CanRun) then {
			if (_active_enemy < _ally_alive && (!_ally_damage || _enemy_troops > 0)) then {
				_dist = 200;
				if (_ally_damage) then {
					_enemy_troops = _enemy_troops - 1;
					_dist = 400;
					diag_log Format["(%1)[%2]: %3: %4: %5, %6",diag_tickTime,"chapter2.sqf","XfMissionChapterExecute","Reinforcement Sent",[_enemy_troops, _active_enemy, _ally_alive,_ally_damage], Time];
				};
				_spawn = [_convoy_pos, _dist] call XfGetRanPointCircleOuter;
				_new_grp = ([d_enemy_side,[_spawn,20],["chapter2_convoy","noveh","aware","buildings"],0] call XfMissionCreateFootmenGroup);
				(leader _new_grp) doMove _convoy_pos;
				_new_grp setSpeedMode "FULL";
				_new_grp addWaypoint [_convoy_pos, 0];
				_enemy_groups set [count _enemy_groups, _new_grp];
			};
		};
	};
	
	_players_found = true;
	_enemy_count = 100;
	While {DM_Mission_Mission_CanRun && _enemy_count > 0 && _players_found} do {
		sleep 5;
		if (X_MP) then {
			_players_found = false;
			{
				if (([(getPos _x), _random_town_pos, _random_town_pos_size * 2] call XfPointInCircle) && isplayer _x) exitWith { _players_found = true; };
			}foreach playableUnits;
		} else {
			_players_found = ([(getPos player), _random_town_pos, _random_town_pos_size * 2] call XfPointInCircle);
		};
		_enemy_count = (([_random_town_pos, _random_town_pos_size * 1.5,d_enemy_side] call XfMissionGetZoneSideCount) select 0);
		if (d_mission_debug_messages) then { diag_log Format["(%1)[%2]: %3: %4: player:%5  enemies:%6",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Chapter End Check",_players_found,_enemy_count]; };
	};
	if (!isnil "DM_Ammo_Box") then {
		if (count DM_Ammo_Box == 3) then {
			["d_r_box", [DM_Ammo_Box]] call XNetCallEvent;
		};
	};
	sleep 15;
	if (DM_Mission_Mission_CanRun) then {
		if (_chapter_succeeded) then {
			call XfMissionChapterSuccess;
		} else {
			call XfMissionChapterFailure;
		};
	} else {
		diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter2.sqf","XfMissionChapterExecute","Mission canceled!"];
	};

	// Just in case a reinforcement is already on the way.
	call XfMissionChapterCleanUp;
	DM_Mission_Chapter_Runs = False;
};

XfMissionChapterSetup={
	private ["_random_town","_random_town_pos","_random_town_pos_size","_troops_power","_troops_coeff","_grpSpecOps","_i"];
	diag_log Format["(%1)[%2]: %3; %4 %5",diag_tickTime,"chapter2.sqf","XfMissionChapterSetup","Starting Initialization:",DM_Mission_Name];
	
	// Units positions
	_mike_camp_units =[
		[[17450.6,6047.5,1067.93],238.683],
		[[17453.3,6033.65,1065.48],244.671],
		[[17454.5,6036.35,1065.84],254.12],
		[[17468.9,6030.79,1068.88],250.957],
		[[17471.1,6023.1,1069.35],230.282],
		[[17504.5,6038.77,1068.01],132.991],
		[[17503.4,6047.26,1071.77],186.121],
		[[17506.9,6045.77,1071.77],103.96],
		[[17503.1,6050.76,1069.19],265.472],
		[[17500.8,6064.59,1069.89],215.875],
		[[17490.6,6062.75,1069.15],305.852],
		[[17501.4,6071.95,1069.89],272.482],
		[[17496.9,6072.52,1069.89],213.152],
		[[17492.5,6075.32,1069.16],12.5253],
		[[17504,6097.47,1069.19],86.0678],
		[[17506.4,6097.48,1072],73.6435],
		[[17458.9,6098.2,1071.78],352.67],
		[[17454.4,6098.1,1071.78],277.534],
		[[17454.8,6097.61,1068.92],273.85],
		[[17465.1,6072.27,1069.24],97.3857],
		[[17468.6,6054.32,1069.23],82.3125],
		[[17466.9,6062.2,1069.89],66.8512],
		[[17465.4,6068.56,1069.89],353.225],
		[[17460.1,6067.88,1069.89],355.953],
		[[17464,6060.99,1069.46],151.523],
		[[17455.6,6062.14,1071.92],260.714],
		[[17458,6055.8,1071.92],177.679],
		[[17467.7,6057.14,1071.92],183.492],
		[[17467.4,6061.66,1071.92],85.1341],
		[[17465.9,6068.85,1071.92],3.50496]
	];

	// Camp empty vehicles
	_mike_camp_vecs =[
		[[17502.3,6082.74,1069.19],270.86],
		[[17501.9,6089.11,1069.19],264.301],
		[[17490.4,6103.34,1068.86],206.443],
		[[17480.2,6106.87,1069.2],177.384],
		[[17467.4,6106.45,1068.77],143.503],
		[[17492.9,6030.54,1067.5],336.418],
		[[17483.7,6030.03,1067.07],352.537]
	];

	// Gate vehicle
	_mike_gate_vec = [[17449.6,6023.8,1064.27],248.932];
	
	_mike_helico = [[17478.6,6090.94,0.1],184.626];
	
	_mike_ammo_box = [17497.1,6082.28,0];
	
	// Center of zone to find a town
	_town_search_center = [18202.4,10635.9,0];
	_towns = [_town_search_center,3500, true] call XfMissionGetTownsInRange;
	if ((count _towns) <= 0) exitWith {"STR_MF_RANDOMTOWNFAILED" call XfMissionMessageToAll; DM_Mission_Mission_CanRun = false; DM_Mission_Chapter_CanRun = false;};

	_camp_mike = [];
	_camp_mike_pos = [];
	
	_parameters = [d_map_locations];
	_parameters set [count _parameters, [XF_MAP_LOCATION_UNIQUENAME,"==","Mike2"]];
	_eligible_towns = _parameters call XfTableFilter;
	if ((count _eligible_towns) <= 0) exitWith {"STR_MF_RANDOMSITEFAILED" call XfMissionMessageToAll; DM_Mission_Mission_CanRun = false; DM_Mission_Chapter_CanRun = false;};
	_camp_mike = _eligible_towns select 0;
	_camp_mike_pos = _camp_mike select XF_MAP_LOCATION_CENTER;
	_camp_mike_size = _camp_mike select XF_MAP_LOCATION_SIZE;

		
	// Destination camp branca
	_parameters = [d_map_locations];
	_parameters set [count _parameters, [XF_MAP_LOCATION_UNIQUENAME,"==","CampBranca"]];
	_eligible_towns = _parameters call XfTableFilter;
	if ((count _eligible_towns) <= 0) exitWith {"STR_MF_RANDOMSITEFAILED" call XfMissionMessageToAll; DM_Mission_Mission_CanRun = false; DM_Mission_Chapter_CanRun = false;};
	_camp_branca = _eligible_towns select 0;
	_camp_branca_pos = _camp_branca select XF_MAP_LOCATION_CENTER;
	
	_random_town = _towns call XfRandomArrayVal;
	_random_town_pos = _random_town select XF_MAP_LOCATION_CENTER;
	_random_town_pos_size = _random_town select XF_MAP_LOCATION_SIZE;
	_random_town_pos_name = _random_town select XF_MAP_LOCATION_DISPLAYNAME;
	
	DM_Chapter_Location = _random_town;
	
	DM_Chapter_Data = [];
	[_random_town_pos, _camp_branca_pos] spawn {
		private ["_start","_end","_min_dist","_road"];
		diag_log Format["(%1)[%2]: %3; %4: %5",diag_tickTime,"chapter2.sqf","XfMissionChapterSetup","Path Calculation",_this];
		_start = _this select 0;
		_end = _this select 1;
		_road = [];
		_min_dist = 10000;
		{ 
			if ((_x distance _start) < _min_dist) then {
				_min_dist = (_x distance _start);
				_road = getPos _x;
			};
		} foreach (_start nearRoads 200);
		_start = _road;
		while { count _start > 0 AND count DM_Chapter_Data <= 0 AND DM_Mission_Chapter_CanRun AND DM_Mission_Mission_CanRun } do {
			DM_Chapter_Data = [_start, _end, 800,1500,360] call XfGetRoadPath;
		};
	};
	
	[false,["chapter2_mike2", _camp_mike_pos, "ELLIPSE", "ColorRed", [_camp_mike_size,_camp_mike_size],"",0,"","BDiagonal",0.5]] call XfMissionMarker;
	[false,["chapter2_town_pos", _random_town_pos, "ELLIPSE", "ColorRed", [_random_town_pos_size * 2,_random_town_pos_size * 2],"",0,"","BDiagonal",0.05]] call XfMissionMarker;
	
	_units_mike = [];
	for [{_l = 0}, {_l < 4}, {_l = _l + 1 }] do {
		_grpSoldiers = [d_own_side,[(getMarkerPos "respawn_west"),0],["chapter2_mike2","noveh","aware","buildings","fortify"],0] call XfMissionCreateFootmenGroup;
		_units_mike = [_units_mike, (units _grpSoldiers)] call XfArrayPushStack;
	};
	_grpHeli = [d_own_side,[(getMarkerPos "respawn_west"),0],[],0,false] call XfMissionCreateFootmenGroup;		
	
	_fnc_KeepInPlace = {
		_unit = _this select 0;
		_unit setUnitPos "Middle";
		_initial_pos = getPos _unit;
		doStop _unit;
		while { alive _unit } do {
			if ((_unit distance _initial_pos) > 2) then { _unit doMove _initial_pos; };
			sleep 1.23456;
		};
	};
	
	[["%1\n\n%2","STR_MF_M_HAMMER1_2_NAME","STR_MF_M_HAMMER1_2_INTRO"],[],8,8] call XfMissionDisplayScreen;	
	sleep 3;
	_free_pos = [17484.1,6038.79,0];
	_pos = [];
	if (X_MP) then {
		{
			if (isPlayer _x) then {
				if (([getPos _x, _camp_mike_pos, _camp_mike_size] call XfPointInCircle) && (_x call XfGetHeight) <= 10) then {
					_pos = (_free_pos findEmptyPosition [0,15]);
					if (count _pos <= 0) then { _pos = [_free_pos, 15] call XfGetRanPointCircle; };
					_x setPos _pos;
				};
			};
		} foreach playableUnits;
	} else {
		if (([getPos player, _camp_mike_pos, _camp_mike_size] call XfPointInCircle) && (player call XfGetHeight) <= 10) then {
			_pos = (_free_pos findEmptyPosition [0,15]);
			if (count _pos <= 0) then { _pos = [_free_pos, 15] call XfGetRanPointCircle; };
			player setPos _pos;
		};
	};
	
	// TODO: Place it over machifazar
	// ["AH64D_EP1",false,d_own_side,[(_mike_gate_vec select 0),0],["chapter2_mike2","CARELESS","YELLOW"]] call XfMissionCreateUnitGroup;
	
	// TODO: Place vehicles inside the base
	
	_i = 0;
	{
		doStop _x;
		if (_i < count _mike_camp_units) then {
			_pos = _mike_camp_units select _i;
			_x setPosASL (_pos select 0);
			_x setDir (_pos select 1);
			_i = _i + 1;
			[_x] spawn _fnc_KeepInPlace;
		} else {
			[_camp_mike_pos, _camp_mike_size] call XfGetRanPointCircleBig;
		};
	} foreach _units_mike;
	
	_heli_info = [_mike_helico, "CH_47F_EP1"] call XfMissionCreateTransportChopper;
	_heli = _heli_info select 0;
	_driver = _heli_info select 1;
	_driver disableAI "MOVE";
	
	{
		_x assignAsCargo _heli;
		_x setPos (getPos _heli);
		_x moveInCargo _heli;
	} foreach units _grpHeli;
	
	_own_side_char = switch (d_own_side) do {case "EAST": {"E"};case "WEST": {"W"};case "GUER": {"G"};case "CIV": {"G"}; default{"E"};};
	_vehicle_array = missionNamespace getVariable format ["d_veh_a_%1",_own_side_char];

	_vec_list = [];
	for [{_l = 0}, {_l < 4}, {_l = _l + 1 }] do {
		_vec_list set[count _vec_list, ((_vehicle_array select XF_VEHICLE_TYPES_TRANSPORT) call XfRandomArrayVal)];
	};
	for [{_l = 0}, {_l < 3}, {_l = _l + 1 }] do {
		_vec_list set[count _vec_list, ((_vehicle_array select XF_VEHICLE_TYPES_LIGHTWHEEL) call XfRandomArrayVal)];
	};
	diag_log Format["(%1)[%2]: %3; %4: %5",diag_tickTime,"chapter2.sqf","XfMissionChapterSetup","Allied Vehicles",_vec_list];
	_enemy_groups = [];
	for [{_l = 0}, {_l < 7}, {_l = _l + 1 }] do {
		_enemy_groups set [count _enemy_groups, ([d_enemy_side,[_random_town_pos,_random_town_pos_size],["chapter2_town_pos","noveh","aware","buildings"],0] call XfMissionCreateFootmenGroup)];
	};
	diag_log Format["(%1)[%2]: %3; %4: %5",diag_tickTime,"chapter2.sqf","XfMissionChapterSetup","Enemy Groups",_enemy_groups];
	
	DM_Ammo_Box = _mike_ammo_box;
	["d_m_box", [DM_Ammo_Box, (nearestObject [DM_Ammo_Box, "AllVehicles"]), "", d_own_side,d_vehicle_drop_box]] call XNetCallEvent;
	
	waituntil { sleep 1.234; (count DM_Chapter_Data > 0) || !DM_Mission_Mission_CanRun || !DM_Mission_Chapter_CanRun };

	if (!DM_Mission_Mission_CanRun || !DM_Mission_Chapter_CanRun) exitWith {};
	
	"STR_MF_M_HAMMER1_2_GETON" call XfMissionMessageToAll;
	
	_positions = [];
	_road = DM_Chapter_Data select 0;
	_i = 1;
	while {count _positions < 12 } do {
		_next_road = DM_Chapter_Data select _i;
		_distance = (_road distance _next_road);
		if (_distance > 20) then {
			_direction = [_next_road, (DM_Chapter_Data select (_i + 1))] call XfDirTo;
			_next_pos = _next_road findEmptyPosition [0,10];
			if (count _next_pos <= 0) then { _next_pos = _next_road;};
			_next_pos set [2, 1.5];
			_positions set [count _positions, [_next_pos, _direction]];
			_road = _next_road;
		};
		_i = _i + 1;
	};
	diag_log Format["(%1)[%2]: %3; %4: %5",diag_tickTime,"chapter2.sqf","XfMissionChapterSetup","Positions",_positions];
	
	_convoy_pos = (_positions select 0) select 0;
	DM_Chapter_Position = _convoy_pos;

	// This indicates where the mission takes place
	[_convoy_pos,80,_random_town_pos_name] call XfMissionCurrentPosition;
	
	for [{_l = 0}, {_l < count _vec_list}, {_l = _l + 1 }] do {
		// Giving parameters to the REGISTER command prevents garbage collection upon destruction until the end of the mission
		_vec = [(_vec_list select _l),((_positions select _l) select 0),"REGISTER",[1,2,3,4],"","NONE"] call XfMissionCreateMissionObject;	
		_vec  setPos ((_positions select _l) select 0);	
		_vec  setDir ((_positions select _l) select 1);
		_vec setVelocity [0.5,0,0.5];
		_vec setDamage 0.8;
		_vec lock true;
		_vec lockturret [[0],true]; 
		if ((_l < 2) || (_l == ((count _vec_list) - 1))) then { _vec setDamage 1; };
	};
	
	["BoardHelicopter","STR_MF_M_HAMMER1_2_TASK_HELI", "STR_MF_M_HAMMER1_2_TASK_HELI_DESC","",[],"Created"] call XfMissionTask;
	
	_start_boarding = 0;
	_player_on_board = false;
	while { !_player_on_board || _start_boarding > time } do {
		_player_on_board = false;
		{
			if (isplayer _x) then {
				_player_on_board = true;
			};
		} foreach crew _heli;
		if (_player_on_board && _start_boarding <= 0) then {
			_start_boarding = time + 30;
			"STR_MF_M_HAMMER1_2_DEPARTING" call XfMissionMessageToAll;
		};
		if (!_player_on_board) then {_start_boarding = 0;};
		sleep 1.9874;
	};
	["BoardHelicopter","", "","",[],"Succeeded"] call XfMissionTask;
	{
		_x enableAI "MOVE";
	} foreach crew _heli;
	
	[true,["chapter2_convoy", _convoy_pos, "ELLIPSE", "ColorBlue", [80,80],"",0,"","",0.5]] call XfMissionMarker;
	[false,["chapter2_convoy_pos", _convoy_pos, "ICON", "ColorBlue", [1,1],"STR_MF_CONVOY",0,"hd_flag","",0.5]] call XfMissionMarker;
	["DefendTheConvoy","STR_MF_M_HAMMER1_2_TASK_DEFEND", ["STR_MF_M_HAMMER1_2_TASK_DEFEND_DESC",_random_town_pos_name],"",[],"Created"] call XfMissionTask;
	
	_direction = [_random_town_pos, _camp_mike_pos] call XfDirTo;
	_turn_pos_x = ((_random_town_pos) select 0) + 500 * (sin _direction);
	_turn_pos_y = ((_random_town_pos) select 1) + 500 * (cos _direction);
	
	_heli_route = [];
	_heli_route set [count _heli_route, _convoy_pos];
	_heli_route set [count _heli_route, [_turn_pos_x,_turn_pos_y,0]];
	_heli_route set [count _heli_route, _convoy_pos];
	_heli_route set [count _heli_route, (_mike_helico select 0)];

	[_heli, _heli_route, (HALOJumpMinimumHeight + 50), true, false] execVM "scripts\mando_heliroute_arma.sqf";
	
	[_heli, _convoy_pos] spawn {
		private ["_vec","_end"];
		_vec = _this select 0;
		_end = _this select 1;
		waituntil { sleep 0.345; ([_vec, _end] call XfDistTo) < 700 };
		"STR_MF_M_HAMMER1_2_APPROACHING" call XfMissionMessageToAll;
		waituntil { sleep 0.345; ([_vec, _end] call XfDistTo) < 200 };
		"STR_MF_M_HAMMER1_2_JUMP" call XfMissionMessageToAll;
		sleep 1.324;
		"STR_MF_M_HAMMER1_2_JUMP" call XfMissionMessageToAll;
		sleep 1.324;
		"STR_MF_M_HAMMER1_2_JUMP" call XfMissionMessageToAll;
		sleep 5;
		waituntil { sleep 0.345; ([_vec, _end] call XfDistTo) < 200 };
		{
			if (_x != driver _vec) then {
				if (!isplayer _x) then {
					_x action ["eject",_vec];
				} else {
					["d_all_exec",["_name = _this select 1; if (_name == str(player)) then { player action ['eject',vehicle player];};",str(_x)]] call XNetCallEvent;
				};
			};
		} foreach crew _vec;
	};
	DM_Target = _enemy_groups;
};

XfMissionChapterSuccess={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter2.sqf","XfMissionChapterSuccess","Starting"];
	"STR_MF_M_HAMMER1_2_SUCCESS" call XfMissionMessageToAll;
	DM_Mission_Chapter_Success = 1;
};

XfMissionChapterFailure={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter2.sqf","XfMissionChapterFailure","Starting"];
	"STR_MF_M_HAMMER1_2_FAILURE" call XfMissionMessageToAll;
	DM_Mission_Chapter_Success = -1;
};

// Self-explanatory
XfMissionChapterCleanUp={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter2.sqf","XfMissionChapterCleanUp","Starting"];
	// Giving an empty position places the teleporter back to the hangar, closing the hangar.
	[[],0] call XfMissionPlaceStartTeleporter;
	[] call XfMissionCleanMarkers;
	[] call XfMissionCleanTriggers;
	[] call XfMissionCleanTasks;
};

DM_Mission_Chapter_Compiled = true;